package com.zzg.test2.web.manage;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.FrameLayout;

import com.zzg.test2.web.WebSetting;

public class WebViewPreloader {
    private static WebView hiddenWebView;
    private static boolean isInitialized = false;

    // 在 Application 或主 Activity 的 onCreate 中调用
    public static void initialize(Context context) {
        if (isInitialized) return;

        // 在主线程中初始化
        new Handler(Looper.getMainLooper()).post(() -> {
            // 创建隐藏的 WebView
            hiddenWebView = new WebView(context);

            // 基础配置
            WebSettings settings = hiddenWebView.getSettings();

            WebSetting.Companion.setWebSetting(hiddenWebView);
//            settings.setJavaScriptEnabled(true);
//            settings.setDomStorageEnabled(true);
//            settings.setCacheMode(WebSettings.LOAD_DEFAULT);
//
//            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);

            // 预加载空白页（避免占用过多资源）
            hiddenWebView.loadUrl("about:blank");

            // 添加到不可见容器（避免视图层次问题）
            FrameLayout container = new FrameLayout(context);
            container.addView(hiddenWebView);
            container.setVisibility(View.GONE);
            container.setLayoutParams(new ViewGroup.LayoutParams(0, 0));

            // 添加到 WindowManager（保持活跃但不显示）
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
//            WindowManager.LayoutParams params = new WindowManager.LayoutParams(
//                    1, 1, // 最小尺寸
//                    Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ?
//                            WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY :
//                            WindowManager.LayoutParams.TYPE_PHONE,
//                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
//                            WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
//                            WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
//                    PixelFormat.TRANSPARENT
//            );
            WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    1, 1,
                    WindowManager.LayoutParams.TYPE_APPLICATION, // 改为 TYPE_APPLICATION
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                            WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
                            WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                    PixelFormat.TRANSPARENT
            );
            windowManager.addView(container, params);

            isInitialized = true;
            Log.d("WebViewPreloader", "WebView preloaded successfully");
        });
    }

    // 获取预初始化的 WebView
    public static WebView getPreloadedWebView(Context context) {
        if (!isInitialized) {
            throw new IllegalStateException("WebViewPreloader not initialized");
        }

        // 返回预加载实例
        return hiddenWebView;
    }
    public static boolean getWebViewIsInitialized() {
       return isInitialized;
    }
    // 释放资源（在不再需要时调用）
    public static void release() {
        if (hiddenWebView != null) {
            new Handler(Looper.getMainLooper()).post(() -> {
                // 清理资源
                hiddenWebView.stopLoading();
                hiddenWebView.setWebViewClient(null);
                hiddenWebView.setWebChromeClient(null);
                hiddenWebView.destroy();
                hiddenWebView = null;

                isInitialized = false;
            });
        }
    }
}